package edu.pku.sei.transformation.editor.model.execution;

import java.util.List;

import org.w3c.dom.Element;

import edu.pku.sei.gefcommon.descriptor.annotation.EnumerationProperty;
import edu.pku.sei.gefcommon.descriptor.annotation.MethodProperty;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.transformation.editor.model.AtomicTransformation;
import edu.pku.sei.transformation.editor.model.ModelPackage;
import edu.pku.sei.transformation.editor.model.Rule;
import edu.pku.sei.transformation.editor.model.RuleInvokeParameter;
import edu.pku.sei.transformation.editor.model.base.NamedElement;

public class RuleExecution extends NamedElement {
	private AtomicTransformationExecution context;
	private Rule specification;
	private int result = RESULT_NONE;
	public static final int RESULT_NONE = 0;
	public static final int RESULT_SUCCESS = 1;
	public static final int RESULT_FAIL = -1;
	
	
	//right now, the three attributes are not used
	private RuleInvokeParameter parameters;
	private List<RuleExecution> preConditionExecutions;
	private List<RuleExecution> postConditionExecutions;
	//end
	
	public AtomicTransformationExecution getContext() {
		return context;
	}
	public void setContext(AtomicTransformationExecution context) {
		this.context = context;
	}
	
	@MethodProperty(id = ModelPackage.RULEEXE_SPCIFICATION, catalog = "Rule Execution", name = "specification", setter = "setSpecification")
	@EnumerationProperty(function = "getRules")
	public Rule getSpecification() {
		return specification;
	}
	public void setSpecification(Rule newValue) {
		if (this.specification != newValue) {
			Rule oldValue = this.specification;
			this.specification = newValue;
			firePropertyChange(ModelPackage.RULEEXE_SPCIFICATION, oldValue, newValue);
		}
	}
	
	public Object[] getRules() {
		if (context != null && context.getTransformation() != null) {
			if (context.getTransformation() instanceof AtomicTransformation)
				return ((AtomicTransformation)context.getTransformation()).getAllRules().toArray();
		}
		return new Object[0];
	}
	
	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeNodeNotContain(root, ModelPackage.RULEEXE_CONTEXT, context);
		XMLUtility.xmlInstance.serializeNodeNotContain(root, ModelPackage.RULEEXE_SPCIFICATION, specification);
	}
	
	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		context = (AtomicTransformationExecution) XMLUtility.xmlInstance.deSerializeNodeNotContain(root, ModelPackage.RULEEXE_CONTEXT, this);
		specification = (Rule) XMLUtility.xmlInstance.deSerializeNodeNotContain(root, ModelPackage.RULEEXE_SPCIFICATION, this);
	}
	
	@Override
	public void dynamicSet(String featureID, Object value) {
		if (ModelPackage.RULEEXE_CONTEXT.equals(featureID)) {
			context = (AtomicTransformationExecution) value;
		} else if (ModelPackage.RULEEXE_SPCIFICATION.equals(featureID)) {
			specification = (Rule) value;
		} else {
			super.dynamicSet(featureID, value);
		}
	}
}
	
	
